package com.example.telnetclient;

import io.termd.core.tty.ReadBuffer;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetOptionHandler;

import java.io.*;

/**
 * @author shanchong
 * @date 2019-06-26
**/
public class TelnetClientDemo {


    public static void main(String[] args) {
        TelnetClient telnetClient = new TelnetClient();
        try {
            telnetClient.connect("localhost",3658);
        } catch (IOException e) {
            e.printStackTrace();
        }
        OutputStream outputStream = telnetClient.getOutputStream();
        InputStream inputStream = telnetClient.getInputStream();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//        try {
//            while (bufferedReader.readLine()!=null){
//                System.out.println(bufferedReader.readLine());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        new Thread(()->{
            InputStream in = System.in;
            BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(in));
            String line=null;
            while (true){
                try {
                    if (!((line = bufferedReader1.readLine())!=null)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    outputStream.write(new String(line+"\n").getBytes());
                    outputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
        try {
            while (bufferedReader.readLine()!=null){
                System.out.println(bufferedReader.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
